<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<head>
    <style type="text/css">
        .mechanics {

        }

        .automation {
            display: none;
        }
    </style>
</head>

<body class="hold-transition skin-blue sidebar-mini">
<!--装配零件信息模态框-->
<div class="modal fade" id="modal-make-choose">
    <div class="modal-dialog" style="width:1350px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">装配零件信息</h4>
            </div>
            <div class="modal-body">
                <form id="queryMake">
                    <div class="row">
                        <div class="form-inline">
                            <input id="prodNoMake" type="hidden" name="prodNo" th:value="${asse.prodNo}">
                            <input id="specialMake" type="hidden" name="special" th:value="${asse.special}">
                            <input id="partIdMake" type="hidden" name="partId" th:value="${asse.partId}">
                            <div class="form-group">
                                <label>图号</label>
                                <input type="text" name="drawNo"
                                       class="form-control"
                                       placeholder="请输入图号">
                            </div>
                            <div class="form-group">
                                <label>图名</label>
                                <input type="text" name="drawName"
                                       class="form-control"
                                       placeholder="请输入图名">
                            </div>
                            <button type="button" class="btn btn-success" id="btn_makeSearch">
                                <span class="Bold">搜索</span>
                            </button>
                            <button type="button" class="btn btn-info pull-right" id="btn_makeChoose">
                                <span class="Bold">确定</span>
                            </button>
                        </div>
                    </div>
                </form>
                <table id="bootstrap-one-choose-table"></table>
            </div>
        </div>
    </div>
</div>
<!--导入模态框-->
<div class="modal fade" data-backdrop="static" data-keyboard="false" id="modal-import-plan">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Excel信息导入</h4>
            </div>
            <div class="modal-body">
                <form id="importForm">
                    <div class="form-group">
                        <label>模版</label>
                        <a class="out" th:href="@{/file/装配外购件导入模板.xlsx}">点击下载</a>
                    </div>
                    <div class="form-group">
                        <label>选择Excel文件并上传</label>
                        <input id="import-file" class="btn" type="file" name="file">
                        <p class="help-block">请按模板格式进行导入，信息与模板信息对应。</p>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button id="btn-import" type="button" class="btn btn-success">
                    <span class="Bold">确定</span>
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<!--参与部门  模态框-->
<div class="modal fade" id="modal-canYuRen-select">
    <div class="modal-dialog" style="width:800px;height: 1000px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">选择部门</h4>
            </div>
            <div class="modal-body">
                <form id="branchForm">
                    <div>
                        <button type="button" style="margin-bottom: 5px" class="btn btn-info pull-left"
                                id="btn-submit-CanYuRen1">添加
                        </button>
                        <table class="table table-bordered" id="bootstrap-table1"></table>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
<section class="content">
    <div class="row" style="margin-bottom: 24px">
        <span style="font-size: 24px" id="h3-drawNo" th:text="${asse.drawNo}"></span>&emsp;&emsp;
        <span style="font-size: 24px" id="h3-drawName" th:text="${asse.drawName}"></span>
        <a id="btn-save" shiro:hasPermission="asseOrd:submit:edit" href="javascript:void(0);"
           class="btn btn-info pull-right" style="margin-right: 10px">
            <span class="Bold">确定</span>
        </a>
    </div>
    <form id="sheetForm" class="form-horizontal">
        <div class="row">
            <div class="col-xs-12" style="margin-left: 4%;padding-right: 6%;">
                <input id="deptId" name="deptId" placeholder="参与科室id" type="hidden" >
                <input type="hidden" name="id" th:value="${asse.id}">
                <table class="t1 t3">
                    <thead>
                    <tr>
                        <td colspan="9">
                            <img th:src="@{/img/logo3.jpg}" width="64px" height="40px" style="margin-top: -30px">
                            <div style="display: inline-block;margin-top: 5px;margin-left: 20px">
                                <span style="letter-spacing: 24px;display: block;line-height: 20px">&nbsp;辽宁忠旺机械设备制造有限公司</span>
                                <span style="display: block;font-size: 12px;line-height: 20px">Liaoning Zhongwang Machinery Equipment Manufacturing Co., Ltd</span>
                            </div>
                        </td>
                        <td colspan="3">装配派工单</td>
                        <td style="width: 3.3%"></td>
                    </tr>
                    <tr>
                        <td style="width: 6%">生产令号</td>
                        <td style="width: 11%">
                            <input id="prodNo" type="text" class="form-control" name="prodNo" th:value="${asse.prodNo}"
                                   readonly>
                        </td>
                        <td style="width: 7.3%">项目名称</td>
                        <td colspan="2">
                            <input id="proCode" name="proCode" type="hidden" th:value="${asse.proCode}">
                            <input id="proUnit" name="proUnit" type="hidden" th:value="${asse.proUnit}">
                            <input id="proName" type="text" class="form-control" name="proName"
                                   th:value="${asse.proName}" readonly>
                        </td>
                        <td style="width: 6%">订单数量</td>
                        <td style="width: 7%">
                            <input id="proNum" type="text" class="form-control" name="proNum" th:value="${asse.proNum}"
                                   readonly>
                        </td>
                        <td style="width: 9.1%">类型</td>
                        <td style="width: 6.9%">
                            <select id="special" class="form-control" name="special" style="padding-left: 5px;">
                                <option value="">请选择</option>
                                <option value="机械" th:selected="${'机械' == asse.special}">机械</option>
                                <option value="流体" th:selected="${'流体' == asse.special}">流体</option>
                            </select>
                        </td>
                        <td style="width: 7.9%">部分</td>
                        <td style="width: 9.8%" colspan="2">
                            <select id="partId" class="form-control" name="partId">
                                <option value="">请选择</option>
                                <option th:each="p : ${parts}" th:value="${p.id}"
                                        th:text="|${p.special}-${p.proPart}|"
                                        th:selected="${p.id == asse.partId}"></option>
                            </select>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td rowspan="2">装配图号</td>
                        <td colspan="2" rowspan="2">
                            <select id="drawNoSelect" class="form-control select2" name="drawNoSelect">
                                <option th:each="d : ${drawNos}" th:value="${d.id}"
                                        th:text="${d.drawNo}" th:selected="${d.drawNo == asse.drawNo}"></option>
                            </select>
                            <input id="drawNo" type="hidden" name="drawNo" th:value="${asse.drawNo}">
                        </td>
                        <td rowspan="2" style="width: 6%">装配名称</td>
                        <td colspan="3" rowspan="2">
                            <input id="drawName" type="text" class="form-control" name="drawName"
                                   th:value="${asse.drawName}" readonly>
                        </td>
                        <td>装配数量</td>
                        <td>
                            <input id="drawNum" type="number" step="0.1" class="form-control" name="drawNum"
                                   th:value="${asse.drawNum}">
                        </td>
                        <td rowspan="2">要求完成时间</td>
                        <td rowspan="2" colspan="2">
                            <input id="compTime" type="text" class="form-control" name="compTime"
                                   th:value="${asse.compTime}" autocomplete="off">
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>重量Kg</td>
                        <td>
                            <input id="drawWeight" type="number" step="0.1" class="form-control"
                                   th:value="${asse.drawWeight}" name="drawWeight">
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>部门</td>
                        <td colspan="11" style="text-align: left;padding-right: 5px;">
                            <span class="out-addDept" title="添加" style="text-align: right;margin-left: 20px"><i
                                    class="fa fa-plus text-green"></i></span>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>路线</td>
                        <td>序号</td>
                        <td>工序</td>
                        <td colspan="9">工艺内容</td>
                        <td></td>
                    </tr>
                    </thead>
                    <tbody id="card">
                    <tr th:each="d : ${details}" class="mechanics">
                        <td>
                            <select class="form-control" name="route" title="" th:if="${d.transFlg == 0}">
                                <option th:each="r : ${routes}" th:value="${r.name}"
                                        th:text="${r.name}" th:selected="${d.route == r.name}"></option>
                            </select>
                            <input type="text" class="form-control" name="route" th:value="${d.route}"
                                   th:if="${d.transFlg > 0}" th:readonly="${d.transFlg > 0}" autocomplete="off">
                        </td>
                        <td>
                            <input type="number" step="1" class="form-control" name="orderNo" th:value="${d.orderNo}"
                                   th:readonly="${d.transFlg > 0}" autocomplete="off">
                        </td>
                        <td>
                            <select class="form-control" name="process" title="" th:if="${d.transFlg == 0}">
                                <option th:each="p : ${processes}" th:value="${p.name}"
                                        th:text="${p.name}" th:selected="${d.process == p.name}"></option>
                            </select>
                            <input type="text" class="form-control" name="process" th:value="${d.process}"
                                   th:if="${d.transFlg > 0}" th:readonly="${d.transFlg > 0}" autocomplete="off">
                        </td>
                        <td colspan="9">
                            <textarea class="form-control" name="comment" th:utext="${d.comment}" rows="1"
                                      th:readonly="${d.transFlg > 0}"></textarea>
                        </td>
                        <td th:style="'display: ' + ${d.transFlg > 0 ? 'none' : ''}">
                            <span class="sheet-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="sheet-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
                    <tr th:if="${details.size()} eq 0" class="mechanics">
                        <td>
                            <select class="form-control" name="route" title="">
                                <option th:each="r : ${routes}" th:value="${r.name}"
                                        th:text="${r.name}"></option>
                            </select>
                        </td>
                        <td>
                            <input type="number" step="1" class="form-control" name="orderNo" autocomplete="off">
                        </td>
                        <td>
                            <select class="form-control" name="process" title="">
                                <option th:each="p : ${processes}" th:value="${p.name}"
                                        th:text="${p.name}"></option>
                            </select>
                        </td>
                        <td colspan="9">
                            <textarea class="form-control" name="comment" rows="1"></textarea>
                        </td>
                        <td>
                            <span class="sheet-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="sheet-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
                    <tr th:if="${details.size()} eq 0" class="mechanics">
                        <td>
                            <select class="form-control" name="route" title="">
                                <option th:each="r : ${routes}" th:value="${r.name}"
                                        th:text="${r.name}" th:selected="${r.name=='转储'}"></option>
                            </select>
                        </td>
                        <td>
                            <input type="number" step="11" class="form-control" name="orderNo" autocomplete="off">
                        </td>
                        <td>
                            <select class="form-control" name="process" title="">
                                <option th:each="p : ${processes}" th:value="${p.name}"
                                        th:text="${p.name}" th:selected="${p.name=='成品'}"></option>
                            </select>
                        </td>
                        <td colspan="9">
                            <textarea class="form-control" name="comment" rows="1"></textarea>
                        </td>
                        <td>
                            <span class="sheet-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="sheet-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
                    <tr class="automation">
                        <td>注<br>意<br>事<br>项</td>
                        <td colspan="11">
                            <textarea id="remark" class="form-control" name="remark" rows="20" th:utext="${asse.remark}"
                                      autocomplete="off"></textarea>
                        </td>
                        <td></td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div class="col-xs-11" style="font-size: 14px;line-height: 200%;margin-left: 4%;">
                <div class="col-xs-3" style="padding-left: 0">提报人： [[${session.user.name}]]</div>
                <div class="col-xs-3 bottom form-inline">联系电话：
                    <input class="form-control" style="width:60%;border: none;border-bottom: 1px solid #000;"
                           type="text" name="createTel" th:value="${asse.createTel}">
                </div>
            </div>
        </div>
    </form>
    <br>
    <hr>
    <div class="row">
        <div class="col-xs-12" style="margin-left: 4%;padding-right: 8.5%;">
            <form id="makeFormList" role="form" enctype="multipart/form-data">
                <h4 class="pull-right" style="margin-bottom: 10px;margin-right: 50%;">装配零件信息</h4>
                <input id="asseAssemblySheetId" name="asseAssemblySheetId" type="hidden" th:value="${asse.id}">
                <button shiro:hasPermission="asseOrd:make:edit" type="button" class="btn btn-info pull-left" id="btn-choose-one" style="margin-left: 4px;"> 选择</button>
                <button shiro:hasPermission="asseOrd:make:edit" type="button" class="btn btn-success pull-left" id="btn-save-one" style="margin-left: 4px;"> 保存</button>
                <div id="hot"></div>
            </form>
        </div>
    </div>
    <br>
    <hr>
    <div class="row">
        <div class="col-xs-12" style="margin-left: 4%;padding-right: 6%;">
            <h4 class="pull-right" style="margin-bottom: 10px;margin-right: 50%;">装配外购件信息</h4>
            <a shiro:hasPermission="asseOrd:buy:edit" href="javascript:void(0);"
               class="btn btn-info pull-left"
               onclick="js.modal.open('modal-import-plan')">
                <span class="Bold">数据导入</span>
            </a>
            <a id="btn-saveBuy" shiro:hasPermission="asseOrd:buy:edit" href="javascript:void(0);"
               class="btn btn-success pull-left" style="margin-left: 4px;">
                <span class="Bold">保存</span>
            </a>
            <form id="buyForm" class="form-horizontal">
                <input type="hidden" name="sheetId" th:value="${asse.id}">
                <table class="t1 t3">
                    <thead>
                    <tr>
                        <td style="width: 4%">序号</td>
                        <td style="width: 25%">物料名称</td>
                        <td style="width: 13%">跟踪号</td>
                        <td style="width: 7%">使用数量</td>
                        <td style="width: 5%">单位</td>
                        <td style="width: 10%">回货状态</td>
                        <td>存放位置</td>
                        <td>备注</td>
                        <td></td>
                    </tr>
                    </thead>
                    <tbody id="out">
                    <tr th:each="d : ${buyList}">
                        <td th:text="${dStat.count}"></td>
                        <td>
                            <input type="text" class="form-control" name="matNameDesc" th:value="${d.matNameDesc}"
                                   autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="trackCode" th:value="${d.trackCode}"
                                   autocomplete="off">
                        </td>
                        <td>
                            <input type="number" class="form-control" name="useNum" th:value="${d.useNum}"
                                   autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="unit" th:value="${d.unit}" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="backStatus" th:value="${d.backStatus}"
                                   autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="position" th:value="${d.position}"
                                   autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="comment" th:value="${d.comment}"
                                   autocomplete="off">
                        </td>
                        <td>
                            <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
                    <tr th:if="${buyList.size()} eq 0">
                        <td>1</td>
                        <td>
                            <input type="text" class="form-control" name="matNameDesc" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="trackCode" autocomplete="off">
                        </td>
                        <td>
                            <input type="number" class="form-control" name="useNum" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="unit" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="backStatus" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="position" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="comment" autocomplete="off">
                        </td>
                        <td>
                            <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </form>
        </div>
    </div>
</section>
<div id="div-route" style="display: none">
    <select class="form-control" name="route" title="">
        <option th:each="r : ${routes}" th:value="${r.name}"
                th:text="${r.name}"></option>
    </select>
</div>
<div id="div-process" style="display: none">
    <select class="form-control" name="process" title="">
        <option th:each="p : ${processes}" th:value="${p.name}"
                th:text="${p.name}"></option>
    </select>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let table1, table3;
    /**可编辑表单数据源*/
    var data = [];
    var tableDept;
    var xinZeng = "xinZeng";
    var canYuCount = 0; //每次添加参与人  计数+1   删除参与人计数-1   用此判断是否添加了部门


    /** 计算表格序号 */
    function computerNo() {
        $("form#buyForm table tbody tr").each(function (i, e) {
            $(this).children("td:first").text(i + 1);
        });
    }

    /** 获取部分 */
    function getPart() {
        let code = $("#proCode").val();
        let type = $("#special").val();
        switch (type) {
            case '机械' :
            case '流体' :
            case '自动化' :
                fetchPart(code, type);
                break;
            case '外观' :
                fetchPart(code, null);
                break;
        }
    }

    /** 获取部分 */
    function fetchPart(code, type) {
        js.get({
            url: ctx + 'assemble/dispatch/partList',
            data: {
                code : code,
                type : type
            },
            success: function (result) {
                $("#partId").empty().append(`<option value="">请选择</option>`);
                result.forEach(value => {
                    if (value) {
                        $("#partId").append(`<option value="${value.id}">${value.special}-${value.proPart}</option>`);
                    }
                })
            }
        })
    }

    /** 清空零件信息 */
    function emptyComp() {
        $("#drawNo").val('');
        $("#drawName").val('');
        $("#drawNum").val('');
        $("#drawWeight").val('');
    }

    /** 路线或注意事项显隐控制 */
    function setControlPage() {
        let specialCon = [[${asse.special}]];
        if (specialCon == "自动化") {
            $(".automation").show();
            $(".mechanics").hide();
        } else {
            $(".automation").hide();
            $(".mechanics").show();
        }

        // 如果存在转序，则主单部分内容不允许修改
        if ([[${transFlg}]] > 0) {
            $('#special').attr("disabled", true);
            $('#partId').attr("disabled", true);
            $('#drawNoSelect').attr("disabled", true);
            $('#drawNum').attr("disabled", true);
        }
    }

    /** 装配零件列表 */
    function loadMakeList() {
        js.post({
            url: ctx + "assemble/assMake/makePieceList",
            data: {asseAssemblySheetId: [[${asse.id}]]},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    for (var i = 0; i < result.data.rows.length; i++) {
                        data.push(result.data.rows[i]);
                    }
                    js.hot.loadData(result.data.rows);

                }
            }
        });
    }

    var process = function (data) {
        if ($("#asseAssemblySheetId").val() == "") {
            js.modal.warning("装配派工单主键为空，请刷新页面后再重新保存！");
            $("#asseAssemblySheetId").focus();
            return;
        }
        js.post({
            url: ctx + "assemble/assMake/saveMakeList",
            data: JSON.stringify(data),
            contentType: "application/json",
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    location.reload();
                } else {
                    js.modal.warning(result.msg);
                }
                js.modal.closeLoading();
            }
        })
    }

    /**
     *全选
     */
    function selectedAll() {
        if ($('.checkboxAll').is(":checked")) {
            $('.checkbox_select').each(function () {
                this.checked = true;
            });
        } else {
            $('.checkbox_select').each(function () {
                this.checked = false;
            });
        }
    }

    /**
     * @Description: 添加部门
     * @Author: wsg
     **/
    function addDept(a1, a2) {
        var xinZengClass = xinZeng + a1;
        //参与人后添加 人名和叉号
        $(".out-addDept").before(`
              <span id="` + xinZengClass + `">&nbsp;&nbsp;&nbsp;` + a2 + `<span class="out-delDept" title="删除" ><i class="fa fa-remove text-red"></i> </span></span>
            `);
        //隐藏input框填 科室id  用 ，分隔
        var branchId = a1;
        var temp1 = $("#deptId").val();
        var temp2 = temp1 + "," + branchId;
        if (temp1 === null || temp1 === "" || temp1 === undefined) {
            $("#deptId").val(temp2.slice(1));
        } else {
            $("#deptId").val(temp2);
        }
        js.modal.hide("modal-canYuRen-select")
        canYuCount++; //每次添加参与人  计数+1   删除参与人计数-1   用此判断是否添加了部门
    }

    $(function () {
        let routeHtml = $("#div-route").html();
        let processHtml = $("#div-process").html();

        let deptList = [[${deptList}]]; //获取参与人对象 数组
        for (let i = 0; i < deptList.length; i++) {
            addDept(deptList[i].id, deptList[i].name); //执行添加参与人方法
        }

        /**增加参与部门*/
        $('#sheetForm').on('click', '.out-addDept', function () {
            js.modal.open("modal-canYuRen-select");
            loadDepts();
        });

        /**移除参与部门*/
        $('#sheetForm').on('click', '.out-delDept', function () {
            //找到要删除的人的 父级 span  id
            var canYuBranchId = $(this).parent().attr('id');
            //删除人名 span
            $("#" + canYuBranchId).remove();

            //删除隐藏input中 branchid
            var delCanYuBranchId = canYuBranchId.slice(7);
            var idValue = $("#deptId").val();
            var BranchIdList = idValue.split(",");
            for (let i = 0; i < BranchIdList.length; i++) {
                if (BranchIdList[i] === delCanYuBranchId) {
                    BranchIdList.splice(i, 1);
                }
            }
            $("#deptId").val(BranchIdList.toString());
            canYuCount--;//每次添加参与人  计数+1   删除参与人计数-1   用此判断是否添加了参与部门
        });

        function loadDepts() {
            /** 加载科长表格 */
            tableDept = js.table.init({
                id: "bootstrap-table1",
                url: ctx + 'assemble/dispatch/deptList',
                showColumns: false,
                showExport: false,
                pageSize: 300,
                pagination: false,
                columns: [
                    {
                        title: '<input class="checkboxAll"  type="checkbox" onclick="selectedAll()" >', width: '40',
                        formatter: function (value, row, index) {
                            return '<input class="checkbox_select"  type="checkbox" name= "checkbox_select" ' +
                                'value= "' + $('<div/>').text(row.id).html() + '" id="' + $('<div/>').text(row.name).html() + '"    >';
                        }
                    },
                    {
                        title: '序号', field: 'id', width: '50',
                        formatter: function (value, row, index, field) {
                            return index + 1;
                        }
                    },
                    {title: "部门名称", field: 'name'}
                ]
            });
        }

        $("#btn-submit-CanYuRen1").click(function () {
            var branchIdList = $("#deptId").val();
            var branchIdListArray = branchIdList.split(",");
            $('input[name="checkbox_select"]:checkbox:checked').each(function (i, value, id) {
                var branchId = $(this).attr("value");
                var branchName = $(this).attr("id");
                for (let j = 0; j < branchIdListArray.length; j++) {
                    if (branchIdListArray[j] === branchId) {
                        js.modal.alert("请勿重复添加！");
                        return;
                    }
                }
                addDept(branchId, branchName);
            });
            js.modal.hide('modal-canYuRen-select');
        });
        /**重置复选框*/
        js.modal.hideEvent("modal-canYuRen-select", function () {
            js.reset("branchForm");
        });

        /** 工艺内容换行处理 */
        $("[name=comment]").each(function () {
            $(this).val(js.textToValue($(this).val()));
        })

        /** 注意事项换行处理 */
        $("[name=remark]").each(function () {
            $(this).val(js.textToValue($(this).val()));
        })

        /** 路线或注意事项显隐控制 */
        setControlPage();

        /** 类型值改变，联动部分 */
        $("#special").change(function () {
            $("#specialMake").val($("#special").val());
            getPart();
        });

        /** 图号下拉框选中 */
        $("#drawNoSelect").on("select2:select", function () {
            let $this = $(this);
            if ($this.val()) {
                js.get({
                    url: ctx + 'assemble/dispatch/drawNo',
                    data: {
                        id: $this.val()
                    },
                    success: function (result) {
                        $("#drawNo").val(result.drawNo);
                        $("#drawName").val(result.drawName);
                        $("#drawNum").val(result.drawNum);
                        $("#h3-drawNo").text(result.drawNo);
                        $("#h3-drawName").text(result.drawName);
                    }
                });
            } else {
                emptyComp();
                $("#h3-drawNo").text('');
            }
        });

        /** 部分下拉框选中 */
        $("#partId").change(function () {
            $("#partIdMake").val($("#partId").val());
            emptyComp();
            let $this = $(this);
            js.get({
                url: ctx + 'assemble/dispatch/drawNoList',
                data: {
                    code : $("#proCode").val(),
                    partId : $this.val(),
                    special : $("#special").val(),
                    drawNo : [[${asse.drawNo}]]
                },
                success: function (result) {
                    $("#drawNoSelect").empty().append(`<option value="">请选择</option>`);
                    result.forEach(value => {
                        if (value) {
                            $("#drawNoSelect").append(`<option value="${value.id}">${value.drawNo}</option>`);
                        }
                    });
                }
            })
        })

        /** 添加一行 */
        $("#sheetForm").on('click', '.sheet-add', function () {
            $(this).parents("tr").after(`
                    <tr class="mechanics">
                        <td>
                            ${routeHtml}
                        </td>
                        <td>
                            <input type="number" step="0.1" class="form-control" name="orderNo" autocomplete="off">
                        </td>
                        <td>
                            ${processHtml}
                        </td>
                        <td colspan="9">
                            <textarea class="form-control" name="comment" rows="1"></textarea>
                        </td>
                        <td>
                            <span class="sheet-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="sheet-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
            `);
            $(this).parents("tr").next().find("select[name=route]").val($(this).parents("tr").find("select[name=route]").val());
            $(this).parents("tr").next().find("input[name=orderNo]").val($(this).parents("tr").find("input[name=orderNo]").val());
        });
        /** 删除行 */
        $('#sheetForm').on('click', '.sheet-del', function () {
            $(this).parents("tr").remove();
            if ($("table tbody tr.mechanics").length === 0) {
                $("#card").append(`
                               <tr class="mechanics">
                                    <td>
                                        ${routeHtml}
                                    </td>
                                    <td>
                                        <input type="number" step="0.1" class="form-control" name="orderNo" autocomplete="off">
                                    </td>
                                    <td>
                                        ${processHtml}
                                    </td>
                                    <td colspan="9">
                                        <textarea class="form-control" name="comment" rows="1"></textarea>
                                    </td>
                                    <td>
                                        <span class="sheet-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                                        <span class="sheet-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                                    </td>
                                </tr>
                            `)
            }
        });

        /** 类型 */
        $('#special').change(function () {
            if ($('#special option:selected').val() == "自动化") {
                $(".automation").show();
                $(".mechanics").hide();
            } else {
                $(".automation").hide();
                $(".mechanics").show();
            }
        });

        /** 保存装配派工单 */
        $("#btn-save").click(function () {
            let $this = $(this);
            js.post({
                url: ctx + "verifyProStatus/isDesignByProCode",
                data: {proCode: $("#proCode").val()},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        let res = js.vd({
                            id: 'sheetForm',
                            notEmpty: ['prodNo', 'proCode', 'partId', 'drawName', 'drawNum', 'createTel']
                        })
                        if (!res) {
                            return;
                        }
                        if (canYuCount <= 0) {
                            js.modal.warning("请添加至少一个部门！");
                            return;
                        }
                        /** 校验类型为机械、流体时，序号不为空 */
                        if ($('#special option:selected').val() != "自动化") {
                            let orderNoList = $("input[name='orderNo']");
                            for (let i = 0; i < orderNoList.length; i++) {
                                if ("" == orderNoList[i].value) {
                                    js.modal.warning("序号不能为空！");
                                    return;
                                }
                            }
                        }
                        $this.attr('disabled', true);
                        js.submit({
                            formId: "sheetForm",
                            url: ctx + 'assemble/dispatch/edit',
                            data: new FormData($("#sheetForm")[0]),
                            success: function (result) {
                                if (result.type === web_status.SUCCESS) {
                                    location.href = ctx + "assemble/dispatch/subPage";
                                } else {
                                    js.modal.warning(result.msg);
                                }
                            }
                        });
                        $this.removeAttr('disabled');
                    } else {
                        let res = js.vd({
                            id: 'sheetForm',
                            notEmpty: ['prodNo', 'proCode', 'drawName', 'drawNum', 'createTel']
                        })
                        if (!res) {
                            return;
                        }
                        if (canYuCount <= 0) {
                            js.modal.warning("请添加至少一个部门！");
                            return;
                        }
                        /** 校验类型为机械、流体时，序号不为空 */
                        if ($('#special option:selected').val() != "自动化") {
                            let orderNoList = $("input[name='orderNo']");
                            for (let i = 0; i < orderNoList.length; i++) {
                                if ("" == orderNoList[i].value) {
                                    js.modal.warning("序号不能为空！");
                                    return;
                                }
                            }
                        }
                        $this.attr('disabled', true);
                        js.submit({
                            formId: "sheetForm",
                            url: ctx + 'assemble/dispatch/edit',
                            data: new FormData($("#sheetForm")[0]),
                            success: function (result) {
                                if (result.type === web_status.SUCCESS) {
                                    location.href = ctx + "assemble/dispatch/subPage";
                                } else {
                                    js.modal.warning(result.msg);
                                }
                            }
                        });
                        $this.removeAttr('disabled');
                    }
                }
            });
        });
        /** ----------以下保存制作件和采购件明细--------------------------------- */

        // 装配零件列表
        loadMakeList();

        /**装配零件信息加载*/
        js.hot.init({
            element: document.getElementById('hot'),
            data: {},
            formData: ["asseAssemblySheetId"],
            colHeaders: ['ID',  '零件图号', '零件名称', '图纸类型', '使用数量', '质检意见', '保存时间',"半成品库ID","原数量"],
            fieldName: ['id',  'drawNo', 'drawName', 'drawType', 'useNum', 'comment', 'commitTime','asseHalfStoreId','oldUseNum'],
            height: 510,
            correctFormat: true,
            minSpareRows: 20,
            disableVisualSelection:'area',
            hiddenColumns: {
                columns: [0,7,8],
                indicators: true
            },
            callBack: process,
            saveButtonId: $("#btn-save-one"),
            columns: [
                {
                    data: 'id',
                    type: 'numeric',
                    width: 40
                },
                {
                    data: 'drawNo',//零件图号
                    type: 'text',
                    width: 150,
                    readOnly: true
                },
                {
                    data: 'drawName',//零件名称
                    type: 'text',
                    width: 200,
                    readOnly: true
                },
                {
                    data: 'drawType',//图纸类型
                    type: 'text',
                    width: 80,
                    readOnly: true
                },
                {
                    data: 'useNum',//使用数量
                    type: 'numeric',
                    width: 80
                },
                {
                    data: 'comment',//质检意见
                    type: 'text',
                    readOnly: true
                },
                {
                    data: 'commitTime',//保存时间
                    type: 'text',
                    width: 80,
                    readOnly: true
                },
                {
                    data: 'asseHalfStoreId',//半成品库ID（隐藏字段）
                    type: 'text',
                    readOnly: true
                },
                {
                    data: 'oldUseNum',//原数量（隐藏字段）
                    type: 'text',
                    readOnly: true
                }
            ]
        })

        /** 打开装配零件信息模态框 */
        $("#btn-choose-one").on('click', function () {
            js.modal.open("modal-make-choose")
            table3 = js.table.init({
                id: "bootstrap-one-choose-table",
                queryForm: "queryMake",
                showExport: false,
                pageSize: 100,
                url: ctx + "assemble/assMake/makeChooseList",
                columns: [
                    {
                        title: '序号', field: 'id', width: '50',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },
                    {title: '图号', field: 'drawNo'},
                    {title: '图名', field: 'drawName'},
                    {title: '库存数量', field: 'useNum'},
                    {title: '材质', field: 'quality'},
                    {title: '重量', field: 'weight'},
                    {title: '规格', field: 'spec'},
                    {title: '备注', field: 'comment'},
                    {title: '存放地点', field: 'storeAddr'},
                    {
                        field: "checked",
                        title: '全选',
                        checkbox: true
                    }
                ]
            })

        });
        /** 批量添加零件 */
        $("#btn_makeChoose").click(function () {
            var rows = js.table.selectRows("bootstrap-one-choose-table");
            // 如果没有选择，显示提示信息
            if (rows.length == 0) {
                js.modal.warning("请至少选择一条明细");
                return false;
            } else {
                // 将选中的每一行数据保存到一个数组中
                // 获取整个表格数据
                for (var i = 0; i < rows.length; i++) {
                    data.unshift(rows[i]);
                }
                let a = data.slice(0);
                js.hot.loadData(a);
                js.modal.hide("modal-make-choose");
            }
        });
        /** 关闭装配零件信息选择模态框 */
        js.modal.hideEvent("modal-make-choose", function () {
            js.table.destroy(table3);
        });

        /** 添加一行 */
        $("#buyForm").on('click', '.out-add', function () {
            $(this).parents("tr").after(`
                    <tr>
                        <td></td>
                        <td>
                            <input type="text" class="form-control" name="matNameDesc" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="trackCode" autocomplete="off">
                        </td>
                        <td>
                            <input type="number" class="form-control" name="useNum" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="unit" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="backStatus" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="position" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="comment" autocomplete="off">
                        </td>
                        <td>
                            <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
            `);
            computerNo();
        });
        /** 删除行 */
        $('#buyForm').on('click', '.out-del', function () {
            $(this).parents("tr").remove();
            if ($("form#buyForm table tbody tr").length === 0) {
                $("#out").append(`
                      <tr>
                        <td></td>
                        <td>
                            <input type="text" class="form-control" name="matNameDesc" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="trackCode" autocomplete="off">
                        </td>
                        <td>
                            <input type="number" class="form-control" name="useNum" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="unit" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="backStatus" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="position" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="comment" autocomplete="off">
                        </td>
                        <td>
                            <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
                            `)
            }
            computerNo();
        });

        /** 导入 */
        $('#btn-import').click(function() {

            let files = $("#import-file")[0].files;
            let fileReader = new FileReader();
            fileReader.onload = function(ev) {
                try {
                    let data = ev.target.result,
                        workbook = XLSX.read(data, {type: 'binary'}), // 以二进制流方式读取得到整份excel表格对象
                        plans = []; // 存储获取到的数据
                    // 遍历每张表读取
                    for (let sheet in workbook.Sheets) {
                        if (workbook.Sheets.hasOwnProperty(sheet)) {
                            plans = plans.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                            break; // 只取第一张表
                        }
                    }
                    if (plans.length === 0) {
                        js.modal.alert("无法读取文件内容");
                    } else {
                        $("#out").empty();
                        /** 动态添加下拉列表的记录数*/
                        plans.forEach((value, index) => {
                            let a = value['使用数量'] || '';
                        if ($.isNumeric(a)){
                            a = Math.floor(a);
                        }
                        let b = value['备注'] || '';
                        let c = value['跟踪号'] || '';
                        let d = value['存放位置'] || '';
                        let m = value['回货状态'] || '';
                        $("#out").append(`
                    <tr>
                        <td></td>
                        <td>
                            <input type="text" class="form-control" name="matNameDesc" value="${value['物料名称']}" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="trackCode" value="${c}" autocomplete="off">
                        </td>
                        <td>
                            <input type="number" class="form-control" name="useNum" value="${a}" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="unit" value="${value['单位']}" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="backStatus" value="${m}" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="position" value="${d}" autocomplete="off">
                        </td>
                        <td>
                            <input type="text" class="form-control" name="comment" value="${b}" autocomplete="off">
                        </td>
                        <td>
                            <span class="out-add" title="添加"><i class="fa fa-plus text-info"></i> </span>
                            <span class="out-del" title="删除"><i class="fa fa-remove text-red"></i> </span>
                        </td>
                    </tr>
                            `);
                    });
                        computerNo();
                        js.modal.hide("modal-import-plan");
                    }
                } catch (e) {
                    js.modal.alert("无法读取文件或文件类型有错误");
                }
            };
            // 以二进制方式打开文件
            fileReader.readAsBinaryString(files[0]);
        });
        /** 保存装配外购件信息 */
        $("#btn-saveBuy").click(function () {
            let $this = $(this);
            let res = js.vd({
                id: 'buyForm',
                notEmpty: ['matNameDesc', 'useNum', 'unit', 'backStatus']
            })
            if (!res) {
                return;
            }
            $this.attr('disabled', true);
            js.submit({
                formId: "buyForm",
                url: ctx + 'assemble/assMake/saveBuy',
                data: new FormData($("#buyForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        window.location.reload();
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
            $this.removeAttr('disabled');
        });

    })

</script>
</body>
</html>
